/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.moxiu.browser;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.internal.preference.YesNoPreference;
import com.moxiu.browser.dialog.BrowserDialog;
import com.moxiu.browser.view.NightToast;
//import com.moxiu.browser.preferences.AdvancedPreferencesFragment;
//import com.moxiu.browser.preferences.BandwidthPreferencesFragment;
//import com.moxiu.browser.preferences.GeneralPreferencesFragment;
//import com.moxiu.browser.preferences.LabPreferencesFragment;
//import com.moxiu.browser.preferences.PrivacySecurityPreferencesFragment;
//import com.moxiu.browser.preferences.WebsiteSettingsFragment;
import com.moxiu.launcher.R;

import java.util.List;

//public class BrowserPreferencesPage extends PreferenceActivity {
//
//	public static final String CURRENT_PAGE = "currentPage";
//	private View view;
//	private DialogPreference historyperfence;
//	private TextView title;
//	
//    @Override
//    public void onCreate(Bundle icicle) { 
//        super.onCreate(icicle);
//        ActionBar actionBar = getActionBar();       
//      if (actionBar != null) {     	      
//    	  actionBar.hide();   
//        }
//      addPreferencesFromResource(R.xml.br_testperfence);
//      
//      setContentView(R.layout.br_ssetting);    
//      
//      PreferenceScreen preferenceScreen = getPreferenceScreen(); 
//      
//      setLayoutResource(preferenceScreen);
//      
//      this.getListView().setDividerHeight(0);  
//      
//      view=(View)findViewById(R.id.moxiu_title_back);     
//      view.setOnClickListener(new OnClickListener() {		
//		@Override
//		public void onClick(View arg0) {
//	          finish();			
//		}
//	});
//    }
//
//   
//    private void setLayoutResource(Preference preference) {
//        if (preference instanceof PreferenceScreen) {
//            PreferenceScreen ps = (PreferenceScreen) preference;              
//            int cnt = ps.getPreferenceCount();
//            for (int i = 0; i < cnt; ++i) {
//                Preference p = ps.getPreference(i);
//                setLayoutResource(p);
//            }
//        } else if (preference instanceof PreferenceCategory) {
//            PreferenceCategory pc = (PreferenceCategory) preference;
//            pc.setLayoutResource(R.layout.br_preference_category);
//            int cnt = pc.getPreferenceCount();
//            for (int i = 0; i < cnt; ++i) {
//                Preference p = pc.getPreference(i);
//                setLayoutResource(p);
//            }
//        } else {	        	
//            preference.setLayoutResource(R.layout.br_pretest);
//        }
//    }
//}

public class BrowserPreferencesPage extends Activity implements OnClickListener{

	public static final String CURRENT_PAGE = "currentPage";
	private View view;
	private DialogPreference historyperfence;
	private TextView title,clear_history,clear_pwd,clear_cache,clear_cookie,settext,titletext,setline,historyline,pwdline,cacheline;
	BrowserSettings settings = BrowserSettings.getInstance(); 
	private FrameLayout NightFrame;
	private  boolean blFlag = false;
	private ImageView backimage;
	private LinearLayout setlayout,settingslayout,safetylayout;
	
    @Override
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle);
        ActionBar actionBar = getActionBar();       
      if (actionBar != null) {     	      
    	  actionBar.hide();   
        }
//      addPreferencesFromResource(R.xml.br_testperfence);
      
      setContentView(R.layout.br_ssetting);    
      init();
  
      NightFrame = (FrameLayout)findViewById(R.id.br_fmsetline);
    SharedPreferences preferences = getSharedPreferences("default_night",
  			MODE_PRIVATE);
  	blFlag = preferences.getBoolean("default_night", false);
  	if(blFlag){		
  		NightFrame.setVisibility(View.VISIBLE);
  		NightModel();
  	}else{
  		NightFrame.setVisibility(View.GONE);
  		getDyeDrawable(R.drawable.br_menu_feedback_black_backhome,false);
  	}

      view.setOnClickListener(new OnClickListener() {		
		@Override
		public void onClick(View arg0) {
	          finish();			
		}
		});
      
     
    }

    public void init(){
        setlayout=(LinearLayout) findViewById(R.id.br_setting_id);
        settingslayout=(LinearLayout)findViewById(R.id.settingslayout);
        view=(View)findViewById(R.id.moxiu_title_back);    
        safetylayout=(LinearLayout)findViewById(R.id.br_safety);
        clear_history=(TextView) findViewById(R.id.clear_history);
        clear_pwd=(TextView) findViewById(R.id.clear_pwd);
        clear_cache=(TextView) findViewById(R.id.clear_cache);
        clear_cookie=(TextView) findViewById(R.id.clear_cookie); 
        
        settext=(TextView)findViewById(R.id.setting);
        titletext=(TextView)findViewById(R.id.title);
        setline=(TextView)findViewById(R.id.setline);
        historyline=(TextView)findViewById(R.id.historyline);
        pwdline=(TextView)findViewById(R.id.pwdline);
        cacheline=(TextView)findViewById(R.id.cacheline);
        backimage=(ImageView)findViewById(R.id.back_image);
        
        clear_history.setOnClickListener(this);
        clear_pwd.setOnClickListener(this);
        clear_cache.setOnClickListener(this);
        clear_cookie.setOnClickListener(this);   	
    }
    public void NightModel(){
  		setlayout.setBackgroundColor(getResources().getColor(R.color.br_night_bg));
  		settingslayout.setBackgroundColor(getResources().getColor(R.color.br_night_bg_on));
  		view.setBackgroundColor(getResources().getColor(R.color.br_night_bg_on));
  		safetylayout.setBackgroundColor(getResources().getColor(R.color.br_night_bg));
  		clear_history.setBackgroundResource(R.drawable.br_night_setting);
  		clear_pwd.setBackgroundResource(R.drawable.br_night_setting);
  		clear_cache.setBackgroundResource(R.drawable.br_night_setting);
  		clear_cookie.setBackgroundResource(R.drawable.br_night_setting);
  		
  		settext.setTextColor(getResources().getColor(R.color.br_night_text));
  		titletext.setTextColor(getResources().getColor(R.color.br_night_text));
  		clear_history.setTextColor(getResources().getColor(R.color.br_night_text));
  		clear_pwd.setTextColor(getResources().getColor(R.color.br_night_text));
  		clear_cache.setTextColor(getResources().getColor(R.color.br_night_text));
  		clear_cookie.setTextColor(getResources().getColor(R.color.br_night_text));
  		
  		setline.setVisibility(View.VISIBLE);
  		historyline.setBackgroundColor(getResources().getColor(R.color.br_night_line));
  		pwdline.setBackgroundColor(getResources().getColor(R.color.br_night_line));
  		cacheline.setBackgroundColor(getResources().getColor(R.color.br_night_line));
  		
  		Drawable backDrawable=getDyeDrawable(R.drawable.br_menu_feedback_black_backhome,true);
  		if(backDrawable!=null){
  			backimage.setImageDrawable(backDrawable);
  		}
    }
	@Override
	public void onClick(View v) {
		showYesNoDialg(v.getId());

	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
   private BrowserDialog yesNoDialog;
   private String key = null;
	void showYesNoDialg(int id) {				
		switch (id) {
			case R.id.clear_history:
				key  = "privacy_clear_history";
				break;
			case R.id.clear_pwd:
				key  = "privacy_clear_passwords";
				break;
			case R.id.clear_cache:
				key  = "privacy_clear_cache";
				break;
			case R.id.clear_cookie:
				key  = "privacy_clear_cookies";
				break;

			default:
				break;
		}
		if (key == null) {
			return;
		}
		
		if (yesNoDialog == null) {
			yesNoDialog = new BrowserDialog(this).yesNoDialog(blFlag);
			yesNoDialog.sureBtn.setText("确定");
			yesNoDialog.sureBtn.setOnClickListener(mDilogListener);
			yesNoDialog.cancelBtn.setOnClickListener(mDilogListener);
		} else if (yesNoDialog.isShowing()){
			yesNoDialog.dismiss();
		}	
		if(key.equals("privacy_clear_history")){    		
			//yesNoDialog.title.setText("清除历史记录");   		
			yesNoDialog.content.setText("确定删除历史记录？");
		 
		}else if(key.equals("privacy_clear_passwords")) {
			//yesNoDialog.title.setText("清除密码");
			yesNoDialog.content.setText("确定删除已保存的密码？");
		}else if(key.equals("privacy_clear_cache")) {
			//yesNoDialog.title.setText("清除缓存");
			yesNoDialog.content.setText("确定删除本地缓存的内容和数据库？");
		}else if(key.equals("privacy_clear_cookies")) {
			//yesNoDialog.title.setText("清除Cookie");
			yesNoDialog.content.setText("确定清除Cookie？");
		}
		yesNoDialog.show();
	}	
	private OnClickListener mDilogListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (yesNoDialog == null || TextUtils.isEmpty(key)) {
				return;
			}
			if (yesNoDialog.sureBtn == v) {
				if (key.equals("privacy_clear_history")) {
					settings.clearHistory();
					 NightToast.makeText(BrowserPreferencesPage.this, R.string.br_clear_history_success, Toast.LENGTH_SHORT)
							.show();
				} else if (key.equals("privacy_clear_passwords")) {
					settings.clearPasswords();

					NightToast.makeText(BrowserPreferencesPage.this, R.string.br_clear_password_success, Toast.LENGTH_SHORT)
							.show();
				} else if (key.equals("privacy_clear_cache")) {
					settings.clearCache();
					settings.clearDatabases();

					NightToast.makeText(BrowserPreferencesPage.this, R.string.br_clear_cache_success, Toast.LENGTH_SHORT)
							.show();
				} else if (key.equals("privacy_clear_cookies")) {
					settings.clearCookies();
					NightToast.makeText(BrowserPreferencesPage.this, R.string.br_clear_cookie_success, Toast.LENGTH_SHORT)
							.show();
				}
				yesNoDialog.dismiss();
			} else if (yesNoDialog.cancelBtn == v) {
				yesNoDialog.dismiss();
			}
		}
	};
	
	protected void onDestroy() {
		super.onDestroy();
		if (yesNoDialog != null && yesNoDialog.isShowing()) {
			yesNoDialog.dismiss();
		}
	}
	
	public  Drawable getDyeDrawable(int drawableId,Boolean isNight) {
		Drawable drawable = null;
		drawable = getResources().getDrawable(drawableId);    
        try {   
        	if(isNight){        	   	
                drawable.setColorFilter(Color.WHITE, Mode.SRC_IN);
                }else{
                drawable.setColorFilter(Color.BLACK, Mode.SRC_IN);	
                }    	
                  
        } catch (OutOfMemoryError error) {
        }
		return drawable;
	} 
}